<?php

defined('BASEPATH') OR exit('No direct script access allowed');

class Qq extends CI_Controller {
    
    private $start_time = 0;//程序启动时间
    private $mysql_start_time = 0;//mysql连接时间
    public function __construct() {
        parent::__construct();
        $this->start_time = time();
        $this->mysql_start_time = time();
        $this->load->library('QqBot');
        include APPPATH.'libraries/QqMessageExec.php';
        $this->load->helper('cli');
    }
    
    public function index()
    {
        $this->login();
        $this->get_info();
        $this->get_message();
    }
    
    public function login(){
        $this->qqbot->doPtLogin();
        $this->qqbot->doQRCode();
        $this->qqbot->displayQRCode($this->qqbot->getQRCode());
        $old = 0;
        do{
                sleep(2);
                $status = $this->qqbot->checkQRCode();
                if($status[0] == 65 && $old != 65){
                        CLI::echos('验证码已过期,重新生成');
                        $this->qqbot->doQRCode();
                        $this->qqbot->displayQRCode($this->qqbot->getQRCode());
                }
                if($status[0] == 66 && $old != 66){
                        CLI::echos('请扫描二维码');
                }
                if($status[0] == 67 && $old != 67){
                        CLI::echos('请在手机上确认');
                }
                $old = $status[0];
        }while(!($status[0] == 0));
        $this->qqbot->doWebQQLogin($status[1]);
        $this->qqbot->doPSessionId();
        $this->qqbot->doVFWebqq();
        $this->qqbot->doHash();
        $this->qqbot->doBkn();
        $this->qqbot->saveCookie();
    }
    
    
    public function get_info()
    {
        
        while(true)
        {
            if($this->qqbot->getSelfInfo())
            {
                CLI::echos('获取用户信息成功');
                break;
            }
            else
            {
                CLI::echos('获取用户信息失败,重新获取中......');
            }
        }
        
        CLI::echos('获取最近联系的好友');
        $this->qqbot->getRecentList(); //最近联系的好友
        
        CLI::echos('获取在线的好友');
        $this->qqbot->getOnlineBuddies();
        
        CLI::echos('获得群列表信息...');
        $this->qqbot->getGroupList();
        
        while(true)
        {
            if($this->qqbot->getFriendList())
            {
                CLI::echos('获取好友信息成功');
                break;
            }
            else
            {
                CLI::echos('获取好友信息失败,重新获取中......');
            }
        }
        
        $account = $this->qqbot->getLoginUserInfo();
        CLI::echos('账户 ' . $account['nick'] . ' 登录成功!');
        CLI::echos('正在初始化消息队列...');
        
        CLI::echos('注册消息处理方法...');
        \PhpQq\WebQq\MessageQueue::getInstance()->setMessageExec('messages','message',new PhpQq\WebQq\QqMessageExec);
        \PhpQq\WebQq\MessageQueue::getInstance()->setMessageExec('exec','message',new PhpQq\WebQq\newExec);
    }
    
    public function get_message()
    {
        CLI::echos('消息队列初始化');
        $ender = $this->qqbot->sender();
        $receiver = $this->qqbot->receiver();
        $ender->start();
        $receiver->start();
        CLI::echos('消息队列初始化成功');
    }
}
